<? 
/*! \file
 *  \brief Construye el codigo de la pagina de presentacion
 *  del monitor.
 *
 *  \author Gustavo Borgoni (gborgoni@despegar.com)
 */

/* Includes files */
include("config.php");
include("functions.php");

if ($_SERVER['REQUEST_METHOD'] == "GET" && isset($_GET['module'])) {
	return get_info($_GET['xml'],$_GET['ip'],$_GET['pbx'],$_GET['port']);
}

/*! \brief Esta funcion devuelve el codigo HTML y JS para
 *  dibujar el monitor en la web.
 */
function html_start() {
	global $pbx;

	$ret  = "<html>\n";
	$ret .= "<head>\n";
	$ret .= "<script src=\"libs/parsexml.js\" type=\"text/javascript\"></script>";
	$ret .= "<link href='themes/default.css' rel='stylesheet' type='text/css'>\n";
	$ret .= "<title>Real Time Monitor</title>\n";
	$ret .= "<script>";
	// Funciones de parseo
	for ($i = 0; $i < count($pbx); $i++) {
		$ret.=write_js($pbx[$i]);
		$ret.=write_calls($pbx[$i],$i);
	}

	$ret .= "</script>";
	$ret .= "</head>\n";
	$ret .= "<body topmargin='0' leftmargin='0'>";
	return $ret;
}

/*! \brief Escribe el codigo js para recuperar el XML
 *  que contiene el estado de las PBX
 *  \param $pbx Array que contiene los datos de la PBX
 */
function write_calls($pbx,$pbx_num) {
	global $modRefresh;

	$url = "index.php?module=getinfo&ip=".$pbx['ip']."&xml=".$pbx['xml']."&port=".$pbx['port']."&pbx=".$pbx_num;

	$ret = "var ".$pbx['name']." = new JKL.ParseXML('".$url."');\n";
	$ret.= "setInterval('load_".$pbx['name']."()', ".$pbx['refresh'].");\n";

	return $ret;
}

/*! \brief 
 *
 *
 */
function write_js ($pbx) {
	$ret = "function load_".$pbx['name']."(){\n";

	$ret.= "var func = function ( data ) {\n";
	
	$ret.= "if(data[\"conf\"][\"aststatus\"] == \"Link Down\"){\n";
	$ret.= "document.getElementById('".$pbx['name']."_linkdown').style.display = '';\n";
	$ret.= "document.getElementById('".$pbx['name']."_linkup').style.display = 'none';\n";
	$ret.= "document.getElementById('".$pbx['name']."_loading').style.display = 'none';\n";
	$ret.= "}else{\n";
	$ret.= "document.getElementById('".$pbx['name']."_linkdown').style.display = 'none';\n";
	$ret.= "document.getElementById('".$pbx['name']."_linkup').style.display = '';\n";
	$ret.= "document.getElementById('".$pbx['name']."_loading').style.display = 'none';\n";
	$ret.= "}\n";

	// Estado de la PBX
	$ret.= "if(data[\"conf\"][\"pbxstatus\"] == \"up\"){\n";
	$ret.= "document.getElementById('".$pbx['name']."_srv_stat').className = 'box_on';\n";
	$ret.= "document.getElementById('".$pbx['name']."_srv_stat_txt').className = 'box_on_txt';\n";
	$ret.= "document.getElementById('".$pbx['name']."_srv_stat_txt').innerHTML = 'on';\n";	
	$ret.= "}else{\n";
	$ret.= "document.getElementById('".$pbx['name']."_srv_stat').className = 'box_off';\n";
	$ret.= "document.getElementById('".$pbx['name']."_srv_stat_txt').innerHTML = 'off';\n";
	$ret.= "}";

	// Estado del Asterisk
	$ret.= "if(data[\"conf\"][\"aststatus\"] == \"up\"){\n";
	$ret.= "document.getElementById('".$pbx['name']."_ast_stat').className = 'box_on';\n";
	$ret.= "document.getElementById('".$pbx['name']."_ast_stat_txt').className = 'box_on_txt';\n";
	$ret.= "document.getElementById('".$pbx['name']."_ast_stat_txt').innerHTML = 'on';\n";
	$ret.= "}else{\n";
	$ret.= "document.getElementById('".$pbx['name']."_ast_stat').className = 'box_off';\n";
        $ret.= "document.getElementById('".$pbx['name']."_ast_stat_txt').className = 'box_off_txt';\n";
	$ret.= "document.getElementById('".$pbx['name']."_ast_stat_txt').innerHTML = 'off';\n";
	$ret.= "}\n";

	// Camales
	$ret.="if(data[\"conf\"][\"channels\"] < 1){\n";
	$ret.="document.getElementById('".$pbx['name']."_channels').className = 'box_off';\n";
	$ret.="document.getElementById('".$pbx['name']."_channels_nu').className='box_off_txt';\n";
	$ret.="document.getElementById('".$pbx['name']."_channels_nu').innerHTML = 000;\n";
	$ret.="}else{\n";
	$ret.="document.getElementById('".$pbx['name']."_channels').className = 'box_on';\n";
	$ret.="document.getElementById('".$pbx['name']."_channels_nu').className='box_on_txt';\n";
	$ret.="document.getElementById('".$pbx['name']."_channels_nu').innerHTML = data[\"conf\"][\"channels\"];\n";
	$ret.="}\n";

	// Calls
        $ret.="if(data[\"conf\"][\"calls\"] < 1){\n";
	$ret.="document.getElementById('".$pbx['name']."_calls').className = 'box_off';\n";
	$ret.="document.getElementById('".$pbx['name']."_calls_nu').className='box_off_txt';\n";
	$ret.="document.getElementById('".$pbx['name']."_calls_nu').innerHTML = 000;\n";
	$ret.="}else{\n";
	$ret.="document.getElementById('".$pbx['name']."_calls').className = 'box_on';\n";
	$ret.="document.getElementById('".$pbx['name']."_calls_nu').className='box_on_txt';\n";
	$ret.="document.getElementById('".$pbx['name']."_calls_nu').innerHTML = data[\"conf\"][\"calls\"];\n";
	$ret.="}\n";

	// IAX!
	foreach ($pbx['iax'] as $key => $value) {
		$ret.="if(data[\"conf\"][\"iax2\"][\"".$key."\"] != \"UNREACHABLE\"){\n";
		$ret.="document.getElementById('".$pbx['name']."_".$key."').className = 'box_on';\n";
		$ret.="document.getElementById('".$pbx['name']."_".$key."').innerHTML = \"".$value.": \"+data[\"conf\"][\"iax2\"][\"".$key."\"];\n";
		$ret.="}else{\n";
		$ret.="document.getElementById('".$pbx['name']."_".$key."').className = 'box_off';\n";
		$ret.="document.getElementById('".$pbx['name']."_".$key."').innerHTML = \"".$value.": down\";\n";
		$ret.="}\n";
	}

	// Localdate
	$ret.="document.getElementById('".$pbx['name'].
		"_date').innerHTML = \"Local date: \"+data[\"conf\"][\"localdate\"]+\" \"+data[\"conf\"][\"localtime\"];\n";

	// Server uptime
	$ret.="document.getElementById('".$pbx['name']."_srv_uptime').innerHTML = \"Server uptime: \" + data[\"conf\"][\"pbxuptime\"];\n";
	// Asterisk uptime
	$ret.="document.getElementById('".$pbx['name']."_ast_uptime').innerHTML = \"Service uptime: \" + data[\"conf\"][\"astuptime\"];\n";
	// Disk!
        $ret.="if (data[\"conf\"][\"disk\"] >= 50 && data[\"conf\"][\"disk\"] < 70) {\n";
	$ret.="document.getElementById('".$pbx['name']."_disk').className = 'disk_alert1'\n";
	$ret.="} else if (data[\"conf\"][\"disk\"] >= 70 && data[\"conf\"][\"disk\"] < 90) {\n";
	$ret.="document.getElementById('".$pbx['name']."_disk').className = 'disk_alert2'\n";
	$ret.="} else if (data[\"conf\"][\"disk\"] >= 90) {\n";
	$ret.="document.getElementById('".$pbx['name']."_disk').className = 'disk_alert3'\n";
	$ret.="}\n";

	$ret.="document.getElementById('".$pbx['name']."_disk').innerHTML = \"Disk usage: \" + data[\"conf\"][\"disk\"] + \"%\";\n";

	$ret.= "} \n".$pbx['name'].".async( func ); \n".$pbx['name'].".parse();	\n }\n";
	return $ret;
}

function new_pbx($pbx,$num,$class) {
	echo "<tr id='".$pbx['name']."_loading'>";
	echo "<td width='80' height='76' class='".$class."' align='right'>".$pbx['name']."</td>\n";
	echo "<td align='Center' colspan='10' class='loading' width='840'>Loading ...</td></tr>";

	echo "<tr id='".$pbx['name']."_linkdown' style=\"display:none\">";
	echo "<td width='80' height='76' class='".$class."' align='right'>".$pbx['name']."</td>\n";
	echo "<td align='Center' colspan='10' class='link_down'>Link Down</td></tr>";

	echo "<tr id='".$pbx['name']."_linkup' style=\"display:none\">\n";
	echo "<td width='80' height='76' class='".$class."' align='right'><strong>".$pbx['name']."</strong><br>(".$pbx['ip'].")</td>\n";

	// -- > Estados
	echo "<td width='120'>\n";
	echo "<table width='100%' border='0' cellpading='1' cellspacing='1'>\n";
	echo "<tr>";
	echo "<td height='36' class='box' id='".$pbx['name']."_srv_stat'>\n";
	echo "<table width='100%' border='0' height='100%' cellpading='0' cellspacing='0'>\n";
	echo "<tr>\n";
	echo "<td valign='top' align='left' class='box_text'>Server Status</td>\n";
	echo "<td class='box_na' align='right' id='".$pbx['name']."_srv_stat_txt'>n/a</td>\n";
	echo "</tr>\n";
	echo "</table>\n";
	echo "</td></tr>\n";
	echo "<tr><td height='36' class='box' id='".$pbx['name']."_ast_stat'>\n";
	echo "<table width='100%' boder='0' height='100%' cellpading='0' cellspacing='0'>\n";
	echo "<tr>\n";
	echo "<td valign='top' align='left' class='box_text'>Service Status</td>\n";
	echo "<td class='box_na' align='right' id='".$pbx['name']."_ast_stat_txt'>n/a</td>\n";
	echo "</tr>\n";
	echo "</table>\n";
	echo "</td></tr>\n";
	echo "</table>\n";
	echo "</td>\n";

	// --> Canales
	echo "<td width='140'>\n";
	echo "<table width='100%' border='0' cellpading='1' cellspacing='1'>\n";
	echo "<tr><td height='36' class='box' id='".$pbx['name']."_channels'>\n";
	echo "<table width='100%' border='0' height='100%' cellpading='0' cellspacing='0'>\n";
	echo "<tr>\n";
	echo "<td valign='top' align='left' class='box_text'>Active Channels</td>\n";
	echo "<td class='box_na' align='right' id='".$pbx['name']."_channels_nu'>000</td>\n";
	echo "</tr>\n";
	echo "</table>\n";
	echo "</td></tr>\n";
	echo "<tr><td height='36' class='box' id='".$pbx['name']."_calls'>\n";
	echo "<table width='100%' border='0' height='100%' cellpading='0' cellspacing='0'>\n";
	echo "<tr>\n";
	echo "<td valign='top' align='left' class='box_text'>Active calls</td>";
	echo "<td class='box_na' align='right' id='".$pbx['name']."_calls_nu'>000</td>";
	echo "</tr>\n";
	echo "</table>\n";
	echo "</td></tr>\n";
	echo "</table>\n";
	echo "</td>\n";

	// --> IAX
	echo "<td valign='top'>\n";
	echo "<table width='100%' border='0' cellpading='0' cellspacing='0'>\n";
	echo "<tr>\n";

	$n = 0;
	foreach ($pbx['iax'] as $key => $value) {
		if ($n%4 == 0 && $n > 0) {
			echo "</table>\n</td>\n";
			echo "<td valign='top'>\n<table cellpading='1' cellspacing='1'>\n";

			echo "<tr>\n<td height='19' width='130' class='box' id='".$pbx['name']."_".$key."' align='right'>".$value."</td>\n</tr>\n";
		} elseif ($n == 0) {
			echo "<td>\n<table cellpading='1' cellspacing='1'>\n";

			echo "<tr>\n<td height='19' width='130' class='box' id='".$pbx['name']."_".$key."' align='right'>".$value."</td>\n</tr>\n";
		} else {
			echo "<tr>\n<td height='19' width='130' class='box' id='".$pbx['name']."_".$key."' align='right'>".$value."</td>\n</tr>\n";
		}

		$n++;
	}
	echo "</table></td></tr>\n";
	echo "</table>\n";
	echo "</td>\n";

	// --> Zaptel TODO
/*	echo "<td>\n";
	if ($pbx['zap'] > 0) {
		echo "<table width='100%' border='0' cellpading='0' cellspacing='0'>\n";
		for ($i = 0, $j=1 ; $i < ($pbx['zap']*4); $i++, $j++) {
			if ($i%4 == 0 && $i > 0) {
				echo "</table>\n</td>\n";
				echo "<td valign='top'>\n<table cellpading='1' cellspacing='1'>\n";
				$j = 1;
				echo "<tr>\n<td height='19' width='80' class='box'>Span ".$j."</td>\n</tr>\n";
			} elseif ($i == 0) {
				echo "<td>\n<table cellpading='1' cellspacing='1'>\n";
				echo "<tr>\n<td height='19' width='80' class='box'>Span ".$j."</td>\n</tr>\n";
			} else {
				echo "<tr>\n<td height='19' width='80' class='box'>Span ".$j."</td>\n</tr>\n";
			}
		}
		echo "</table></td></tr>";
		echo "</table>\n";
	}
	echo "</td>\n";
*/
	// --> General info
	echo "<td width='240'>\n";
	echo "<table width='100%' border='0' cellpading='1' cellspacing='1'>";
	echo "<tr><td align='left' height='19' class='box_info' id='".$pbx['name']."_date'>Local date: 00-00-0000 00:00</td></tr>";
	echo "<tr><td align='left' height='19' class='box_info' id='".$pbx['name']."_srv_uptime'>Server uptime: 00 days 00:00</td></tr>";
	echo "<tr><td align='left' height='19' class='box_info' id='".$pbx['name']."_ast_uptime'>Service uptime: 00 days 00:00</td></tr>";
	echo "<tr><td align='left' height='19' class='box_info' id='".$pbx['name']."_disk'>Disk usage: 00%</td></tr>";
	echo "</table>\n";
	echo "</td>\n";
	echo "</tr>\n";
}
	
	echo html_start();

	echo "<table border=0 cllpading='0' cellspacing='0'>";
	echo "<tr>";
	echo "<td><img src=images/logo.jpg></td>";
	echo "</tr>";
	echo "</table>";
	echo "<br>";
	echo "<table border='0' cellpadding='1' cellspacing='1'>";
	echo "<tr>";
	echo "<td width='120' align='center' class='pbx_titles'>Desc.</td>";
	echo "<td width='120' align='center' class='pbx_titles'>service</td>";
	echo "<td width='120' align='center' class='pbx_titles'>channels</td>";
	echo "<td align='center' class='pbx_titles'>iax trunks</td>";
	//echo "<td width='80' align='center' class='pbx_titles'>zap trunks</td>";
	echo "<td width='240' align='center' class='pbx_titles'>general info.</td>";
	echo "</tr>";

	for ($i = 0; $i < count($pbx); $i++) {
		if ($x == 1) {
			$class = '';
			$x = 0;
		} else {
			$class = 'pbx_descript';
			$x = 1;
		}

		new_pbx($pbx[$i],$i,$class);
	}
	echo "</table>";

	echo close_html();
?>
